﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuditModel;
using AuditWeb.Models;
using System.Data;

namespace AuditWeb.Controllers
{
    public class CountyAdminController : Controller
    {
        IAnnualEventBL aebl = new AnnualEventBL();
        IArchiveBL iabl = new ArchiveBL();
        AuditDBEntities db = new AuditDBEntities();
        ICandidateQuotaBL icqbl = new CandidateQuotaBL();

        //
        // GET: /CountyAdmin/
        [Authorize(Roles = "区县管理员")]
        public ViewResult Index()
        {
            //ViewBag.ActiveAnnualEvents = new SelectList(aebl.GetActiveEvents(), "Id", "Title");
            //string eventid = aebl.GetActiveEvents().First().ID;

            //return RedirectToAction("Index", "Archive", new { id = eventid });
            AnnualEvent currentevent = aebl.GetActiveEvents().FirstOrDefault();
            ViewBag.AnnualEvent = currentevent;
            ViewBag.AnnualEventId = currentevent.ID;
            
            string countycode = HttpContext.Profile["CountyCode"].ToString();
            ViewBag.CountyName = db.Counties.SingleOrDefault(c => c.Code == countycode).Name;
            var countycqlist = icqbl.GetCandidateQuotasTotal(currentevent.ID, -1, countycode);
            int CandidateQuotaCount = (from cq in countycqlist select cq).Sum(cq=>cq.Quota).Value;
            int ArchiveCount = (from cq in countycqlist select cq).Sum(cq=>cq.Total).Value;
            int ArchiveWithTimeCount = (from a in db.Archives where a.CountyCode == countycode && a.ClassTime != null select a).Count();

            ViewBag.ArchiveCount = ArchiveCount;
            ViewBag.CandidateQuotaCount = CandidateQuotaCount;
            ViewBag.ArchiveWithTimeCount = ArchiveWithTimeCount;

            var archives = from a in db.Archives where a.CountyCode==countycode select a;
            //var ccddates = from ccd in db.CountyClassDates where ccd.CountyCode == countycode select ccd.ClassDate;
            var ClassDates = (from ccd in db.CountyClassDates where ccd.CountyCode == countycode select new PWCountyClassDate() { CountyName = ccd.County.Name, ClassDate = ccd.ClassDate, CountyCode = ccd.CountyCode }).Distinct();
            ViewBag.ClassDateList = ClassDates.ToList();
            
            return View();
        }

        [Authorize(Roles = "区县管理员")]
        public ViewResult CandidateQuota()
        {
            //ViewBag.ActiveAnnualEvents = new SelectList(aebl.GetActiveEvents(), "Id", "Title");
            //string eventid = aebl.GetActiveEvents().First().ID;

            //return RedirectToAction("Index", "Archive", new { id = eventid });
            AnnualEvent currentevent = aebl.GetActiveEvents().FirstOrDefault();
            ViewBag.AnnualEvent = currentevent;
            ViewBag.AnnualEventId = currentevent.ID;

            string countycode = HttpContext.Profile["CountyCode"].ToString();
            ViewBag.CountyName = db.Counties.SingleOrDefault(c => c.Code == countycode).Name;

            var cqlist = icqbl.GetCandidateQuotasTotal(currentevent.ID, -1, countycode);

            return View(cqlist);
        }

        [HttpPost]
        [Authorize(Roles = "区县管理员")]
        public ActionResult SetClassDateTime(AdminArchiveModel aam)
        {
            if (ModelState.IsValid)
            {
                string annualeventid = aam.AnnualEventId;
                string s = Request.Form.ToString();
                string[] nv = s.Split('&');
                //string datetype = Request.Form["DateType"];
                //DateTime date = DateTime.Parse(Request.Form["Date"]);
                foreach (string ss in nv)
                {
                    string archiveid = ss.Split('=')[0];
                    string v = ss.Split('=')[1];
                    if (v == "true")
                    {
                        Archive archive = db.Archives.SingleOrDefault(a => a.ID == archiveid);
                        if (archive != null)
                        {
                            switch (aam.DateType)
                            {
                                case "0":
                                    archive.ClassDate = aam.Date;
                                    archive.ClassTime = aam.Time;
                                    if (aam.Date != archive.EvaluationStartDate)
                                    {
                                        archive.EvaluationStartDate = aam.Date;
                                        archive.EvaluationDueDate = aam.Date.AddDays(2);
                                    }
                                    break;
                                default:
                                    break;
                            }
                            //db.Archives.Attach(archive);
                            db.ObjectStateManager.ChangeObjectState(archive, EntityState.Modified);
                            db.SaveChanges();
                        }
                    }
                }
            }

            return RedirectToAction("Index","Archive", new { id = aam.AnnualEventId, archiveid = aam.ArchiveId, countycode = aam.CountyCode, subjectid = aam.SubjctId });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        
    }
}
